<?php

class session_redis {
//    Redis连接对象
    private $redis;
//    Session过期时间
    private $expire;

    private $config; //yyy 全局配置

    public function __construct() {
        //yyy 的全局配置文件
        $this->config = require_once('../application/Common/Conf/config.php');
    }

    /**
     * 打开方法
     * @param type $path
     * @param type $name
     * @return type
     */
    public function open($path, $name) {
        $this->expire = $this->config['SESSION_EXPIRE'] ? $this->config['SESSION_EXPIRE'] : ini_get('session.gc_maxLifetime');
        $this->redis = new Redis();

        return $this->redis->connect($this->config['REDIS_HOST'], $this->config['REDIS_PORT']);
    }

    /**
     * 关闭
     * @return type
     */
    public function close() {
        return $this->redis->close();
    }

    /**
     * 读取
     * @param string $id
     * @return type
     */
    public function read($id) {
        $id = $this->config['SESSION_PREFIX'] . $id;
        $data = $this->redis->get($id);
        return $data ? $data : '';
    }

    /**
     * 写入
     * @param string $id
     * @param type $data
     * @return type
     */
    public function write($id, $data) {   
        $id = $this->config['SESSION_PREFIX'] . $id;
        return $this->redis->set($id, $data, $this->expire);
    }

    /**
     * 销毁
     * @param string $id
     */
    public function destroy($id) {
        $id = $this->config['SESSION_PREFIX'] . $id;
        $this->redis->delete($id);
    }

    /**
     * 垃圾回收
     * @param type $maxLifeTime
     * @return boolean
     */
    public function gc($maxLifeTime) {
        return true;
    }
}
